# -*- coding:utf-8 -*-
from typing import Union, Any, Sequence, List, Dict, Tuple
from dataclasses import dataclass

from numpy import ndarray
from dataclasses_json import dataclass_json

DataType = Any
ImageType = Union[ndarray, None]
PixelType = Union[int, float]
ScoreType = float


@dataclass_json
@dataclass
class BaseBox(object):
    x1: PixelType = 0
    y1: PixelType = 0
    x2: PixelType = 0
    y2: PixelType = 0

    def __call__(self, *args, **kwargs):
        return self.ltrb

    def set_ltrb(self, target: List):
        self.x1 = target[0]
        self.y1 = target[1]
        self.x2 = target[2]
        self.y2 = target[3]

    @property
    def ltrb(self) -> list:
        return [self.x1, self.y1, self.x2, self.y2]

    @property
    def width(self) -> PixelType:
        return abs(self.x2 - self.x1)

    @property
    def height(self) -> PixelType:
        return abs(self.y2 - self.y1)

    @property
    def center_x(self) -> float:
        return (self.x1 + self.x2) / 2

    @property
    def center_y(self) -> float:
        return (self.y1 + self.y2) / 2


@dataclass_json
@dataclass
class DetBox(BaseBox):
    score: ScoreType = 0.0
    class_id: Any

    def __init__(self, target: List):
        self.set_ltrb(target)

    def set_ltrb(self, target: List):
        self.x1 = target[0]
        self.y1 = target[1]
        self.x2 = target[2]
        self.y2 = target[3]
        self.score = target[4]
        self.class_id = target[5]

    @property
    def ltrbs(self) -> list:
        return [self.x1, self.y1, self.x2, self.y2, self.score, self.class_id]
